package com.liukai.bishe.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.liukai.bishe.R;
import com.liukai.bishe.model.doman.transaction.Transaction;
import com.liukai.bishe.presenter.transactionPresenter.ITransactionPresenter;
import com.liukai.bishe.ui.viewHolder.BaseViewHolder;
import com.liukai.bishe.ui.viewHolder.TransactionViewHolder;
import com.liukai.bishe.ui.viewHolder.listener.TransactionOnClickListener;
import com.liukai.bishe.ui.viewHolder.listener.impl.TransactionOnClickListenerImpl;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class TransationAdapter extends RecyclerView.Adapter<BaseViewHolder>{

    private Context mContext;

    private TransactionOnClickListener mTransactionOnClickListener;// 事务点击事件监听的实现类

    private List<Transaction> mTransactionList;

    private ITransactionPresenter mITransactionPresenter;

    public TransationAdapter(){

    }

    public TransationAdapter(Context context, List<Transaction> transactionList, ITransactionPresenter iTransactionPresenter) {
        this.mContext = context;
        mTransactionOnClickListener = new TransactionOnClickListenerImpl(mContext);
        mTransactionList = transactionList;
        mITransactionPresenter = iTransactionPresenter;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(),R.layout.item_recyclerview_transaction,null);
        return new TransactionViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {

        Transaction transaction = mTransactionList.get(position);
        TransactionViewHolder transactionViewHolder = (TransactionViewHolder)holder;
        transactionViewHolder.transactionTime.setText(transaction.getTime() + "分钟");
        transactionViewHolder.transactionTitle.setText(transaction.getTransactionname());

        // 开始按钮点击事件
        transactionViewHolder.btnTransactionStart.setOnClickListener(view ->
                mTransactionOnClickListener.onStartTransationListener(
                        transactionViewHolder.btnTransactionStart,
                        transaction));

        // 事务长按事件
        transactionViewHolder.itemView.setOnLongClickListener(view -> {
            mTransactionOnClickListener.onItemViewOnLongClickListener(
                    transactionViewHolder.itemView,
                    transaction,
                    mITransactionPresenter);
            return true;
        });

        transactionViewHolder.itemView.setOnClickListener(view -> {
            mTransactionOnClickListener.onItemViewOnLongClickListener(
                    transactionViewHolder.itemView,
                    transaction,
                    mITransactionPresenter);
        });

        transactionViewHolder.mRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTransactionOnClickListener.onItemViewOnLongClickListener(
                        transactionViewHolder.itemView,
                        transaction,
                        mITransactionPresenter);
            }
        });

        transactionViewHolder.mRelativeLayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                mTransactionOnClickListener.onItemViewOnLongClickListener(
                        transactionViewHolder.itemView,
                        transaction,
                        mITransactionPresenter);
                return true;
            }
        });

    }

    @Override
    public int getItemCount() {
        return mTransactionList.size();
    }


    public void setTransactionList(List<Transaction> transactionList) {
        this.mTransactionList.clear();
        this.mTransactionList.addAll(transactionList);
    }
}
